package com.hailiao.beidou.message.bd.task

import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.ProtoVersion
import com.hailiao.beidou.standard2.CCRMO_Data
import com.hailiao.beidou.standard2.RMO_Action
import com.hailiao.beidou.standard2.sendCCRMO
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * 检查信号任务
 * @Author d10ng
 * @Date 2024/3/5 18:00
 */
object CheckSignalTask {

    private var job: Job? = null
    private val scope = CoroutineScope(Dispatchers.IO)

    @Synchronized
    fun start(ver: ProtoVersion) {
        job?.cancel()
        job = scope.launch {
            while (true) {
                val cmd = when(ver) {
                    ProtoVersion.BD2 -> "BSI"
                    ProtoVersion.BD3 -> "PWI"
                }
                Beidou.hub.sendCCRMO(CCRMO_Data(cmd, RMO_Action.OPEN, 2))
                delay(2 * 1000)
            }
        }
    }

    @Synchronized
    fun stop() {
        job?.cancel()
    }
}